﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ViewTagsCommand.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="AddAccountCommand" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Tag.Commands
{
    using System;
    using System.Windows.Input;

    using Hisaab.Client.Common;
    using Hisaab.Client.Common.Events;
    using Hisaab.Client.Modules.Core.Account;
    using Hisaab.Client.Modules.Core.Tag.ViewModel;

    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="ViewTagsCommand"/> type.
    /// </summary>
    public class ViewTagsCommand : ICommand
    {
        #region Fields

        /// <summary>
        /// Member variable m_ViewModel
        /// </summary>
        private TagViewButtonViewModel _viewModel;

        #endregion Fields

        /// <summary>
        /// Initializes a new instance of the <see cref="ViewTagsCommand"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        public ViewTagsCommand(TagViewButtonViewModel viewModel)
        {
            _viewModel = viewModel;
        }

        /// <summary>
        /// Actions to take when CanExecute changes.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            // Initialize
            var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

            // Show Workspace
            var dashboardWorkspace = new Uri(TagModule.TagsView, UriKind.Relative);
            regionManager.RequestNavigate(Regions.WorkspaceRegion, dashboardWorkspace, NavigationCompleted);
        }

        /// <summary>
        /// Defines the method that determines whether the ShowModuleAViewCommand is enabled.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        #region Private Methods

        /// <summary>
        /// Callback method invoked when navigation has completed.
        /// </summary>
        /// <param name="result">Provides information about the result of the navigation.</param>
        private void NavigationCompleted(NavigationResult result)
        {
            if (result.Result != true)
            {
                return;
            }

            // Publish ViewRequestedEvent
            var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
            var navigationCompletedEvent = eventAggregator.GetEvent<NavigationCompletedEvent>();
            navigationCompletedEvent.Publish(AccountModule.AccountTab);
        }

        #endregion Private Methods
    }
}